-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Allow initializing NonZero
with literals
#143594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
oli-obk
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
oli-obk:non-zero-literals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We'll also need to update the Reference for this, and the details in that PR would help form another basis for the FCP here. cc @ehuss |
oli-obk
commented
Jul 7, 2025
|
||
fn main() { | ||
let _: std::num::NonZero<u8> = 256; | ||
//~^ ERROR literal out of range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the erroring example
cf76d4e
to
8797188
Compare
8797188
to
94dadf4
Compare
rust-bors bot
added a commit
that referenced
this pull request
Jul 8, 2025
Limited nonzero coercion coercing `NonZero<T>` to `T` if all the types are available at the cast site. This is fairly limited, as (as the tests show), this does not allow "reborrowing" `&NonZero<T>` as `&T`. It also fails quickly if the types aren't fully clear at the coercion site, but only resolved later. Also cannot aid in trait selection or similar, even if there is clearly only one option. Related: * #143594 * rust-lang/rfcs#3786 cc `@traviscross`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
needs-fcp
This change is insta-stable, or significant enough to need a team FCP to proceed.
S-waiting-on-documentation
Status: Waiting on approved PRs to documentation before merging
T-lang
Relevant to the language team
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This basically implements rust-lang/rfcs#3786 by teaching the language about
NonZero
and the literalsNonZero
supports.I believe it is a minor enough feature that it either doesn't need an RFC and can just be FCPed or it is a simple enough RFC that @rust-lang/lang could FCP it to set a precedent for doing this for other libcore types. Anything like this for user types would of course require a full RFC as there are many open questions around it that
NonZero<T>
just doesn't have.